package com.example.demo.service.impl;

import com.example.demo.mapper.HistoryOrderMapper;
import com.example.demo.pojo.HistoryOrder;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

/**
 * (HistoryOrder)表服务实现类
 *
 * @author makejava
 * @since 2025-01-20 15:52:02
 */
@Service("historyOrderService")
public class HistoryOrderServiceImpl {
    @Resource
    private HistoryOrderMapper historyOrderMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param historyId 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer historyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", this.historyOrderMapper.selectById(historyId));
        map.put("msg", "查询单条记录成功");
        return map;
    }

    /**
     * 一次性全部查询
     *
     * @return 查询结果
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "全部查询成功");
        map.put("data", this.historyOrderMapper.selectAll());
        return map;
    }

    /**
     * 分页查询
     *
     * @param historyOrder 筛选条件
     * @param page        要查询第几页
     * @param limit       每页显示多少条
     * @return 查询结果
     */
    public Map<String, Object> selectByLimit(HistoryOrder historyOrder, int page, int limit) {
        // page 页码   (page-1)*limit=起始下标index
        int startIndex = (page - 1) * limit;   // 计算出起始下标
        List<HistoryOrder> list = this.historyOrderMapper.selectByLimit(historyOrder, startIndex, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");

        // 获取满足条件的总条数
        long count = this.historyOrderMapper.selectCount(historyOrder);
        // 计算总页数
        long maxPage = (count - 1) / limit + 1;

        map.put("count", count);
        map.put("maxPage", maxPage);
        map.put("nowPage", page);
        map.put("data", list);
        return map;
    }
    
    /**
     * 新增数据
     *
     * @param historyOrder 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(HistoryOrder historyOrder) {
        this.historyOrderMapper.insert(historyOrder);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", historyOrder);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 修改数据
     *
     * @param historyOrder 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(HistoryOrder historyOrder) {
        this.historyOrderMapper.updateById(historyOrder);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", historyOrder);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param historyId 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(Integer historyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("id", historyId);
        map.put("msg", this.historyOrderMapper.deleteById(historyId) > 0 ? "删除成功" : "删除失败");
        return map;
    }
}
